﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EngineOfEvermore.Common
{
    public sealed class FrameUpdater
    {
        #region Variables
        int _targetFps = 0;
        double _inverseTargetFps;
        double _timeAccu = 0.0;
        double _lastTime = 0.0;
        ModifiableLogicUpdateEventArgs _e = new ModifiableLogicUpdateEventArgs();
        AgateLib.Timing.StopWatch _timer = new AgateLib.Timing.StopWatch();
        #endregion

        #region Properties
        /// <summary>
        /// The target update rate in frames per second.
        /// </summary>
        /// <remarks>
        /// If set to zero the update is executed immediately.
        /// </remarks>
        public int TargetFps
        {
            get { return _targetFps; }
            set
            {
                _targetFps = value;

                if ( _targetFps != 0 )
                {
                    _inverseTargetFps = 1.0 / value;
                    _e.SetIsFixedTimeStep( true );
                    _e.SetElapsedTime( (float)_inverseTargetFps );
                }
                else
                {
                    _e.SetIsFixedTimeStep( false );
                }
            }
        }
        #endregion

        #region Constructor
        public FrameUpdater()
        {
            _timer.Reset();
			_timer.Resume();
        }
        #endregion

        #region Methods
        public void Update( Action<LogicUpdateEventArgs> updateAction )
        {
            double currentTime = _timer.TotalSeconds;
            double elapsedTime = currentTime - _lastTime;
            _lastTime = currentTime;

            // If there is a target framerate specified only update if necessary
            if ( _targetFps != 0 )
            {
                _timeAccu += elapsedTime;

                while ( _timeAccu > _inverseTargetFps )
                {
                    updateAction( _e );
                    _timeAccu -= _inverseTargetFps;
                }
            }
            else
            {
                // else just propagate the update
                _e.SetElapsedTime( (float)elapsedTime );
                updateAction( _e );
            }
        }
        #endregion
    }
}
